package tm.testing;

import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.curve.Curve;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.export.xml.XMLImporter;

public class SuburbanTrain extends Vehicle {

	private Node geom;
	private TrainController tc;
	
	public SuburbanTrain(String id, Curve curve, Node n) {
		super(id, curve, n);
		geom = n;
		initTrainController();
	}
	
	protected void initMotionController() {
		// Overrides initialization of CurveController,
		// using TrainController instead.
	}
	
	private void initTrainController() {
		tc = new TrainController(curve, geom);
	}
	
	public void update() {
		tc.update();
		/*
		switch(motionState) {
		case IDLE:
			tc.setSpeed(0);
			break;
		case DRIVING:
			if(tc.getSpeed()<maxSpeed) {
				tc.setSpeed(tc.getSpeed()+acceleration+(tc.getSpeed()*0.05f));
			}
			break;
		}
		*/	
	}
	
	public static Node loadXMLModel(String file) {
		XMLImporter xmlImporter = XMLImporter.getInstance();
		URL url=Road.class.getClassLoader().getResource("data/models/traintest-jme.xml");
        Node loadedNode = null;
        try {
        	loadedNode = (Node) xmlImporter.load(url);
        } catch (Exception e) {
            throw new IllegalArgumentException(
                    "Failed to load URL: " + url, e);
        }
        //loadedNode.setLocalScale(10f);        	       
        loadedNode.setModelBound(new BoundingBox());
        /*
         * This setModelBound is not suitable for general usage.
         * If the model file contains specific model bounds for nested
         * Geometries, this will replace them all with new BoundingBoxes.
         * Since the HottBJ Exporter doesn't store bounding volumes
         * yet, that's ok, but there's also no opportunity to choose a
         * BoundingSphere or OBB instead of a plain box.
         */
        loadedNode.updateModelBound();
        return loadedNode;
	}
	
	public static Spatial loadSimpleXMLModel(String file) {
		XMLImporter xmlImporter = XMLImporter.getInstance();
		URL url=Road.class.getClassLoader().getResource("data/models/traintest-jme.xml");
        Spatial loadedSpatial = null;
        try {
            loadedSpatial = (Spatial) xmlImporter.load(url);
        } catch (Exception e) {
            throw new IllegalArgumentException(
                    "Failed to load URL: " + url, e);
        }       	       
        loadedSpatial.setModelBound(new BoundingBox());
        /*
         * This setModelBound is not suitable for general usage.
         * If the model file contains specific model bounds for nested
         * Geometries, this will replace them all with new BoundingBoxes.
         * Since the HottBJ Exporter doesn't store bounding volumes
         * yet, that's ok, but there's also no opportunity to choose a
         * BoundingSphere or OBB instead of a plain box.
         */
        loadedSpatial.updateModelBound();
        return loadedSpatial;
	}	
}